package com.example.photography.adapter.message;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.photography.R;
import com.example.photography.adapter.community.CommunityAdapter;
import com.example.photography.bean.MessagechatBean;
import com.example.photography.ui.CircleImageView;
import com.example.photography.util.Util;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 消息首页适配器
 */
public class MessageChatAdatper extends RecyclerView.Adapter {
    /**
     * 推送信息
     */
    public static final int PUSH = 0;

    /**
     * 系统信息
     */
    public static final int SYS = 1;

    /**
     * 聊天信息
     */
    public static final int CHAT = 2;
    //类型
    private int currentType = 0;
    /**
     * 上下文对象
     */
    private Context context;
    /**
     * 初始化布局
     */
    private final LayoutInflater layoutInflater;
    private MessagechatBean messageChat;

    public MessageChatAdatper(Context context, MessagechatBean messageChat) {
        this.context = context;
        this.messageChat = messageChat;
        layoutInflater = LayoutInflater.from(context);
    }

    /**
     * 相当于getView 创建viewHolder部分代码
     *
     * @param parent
     * @param viewType 当前的类型
     * @return
     */
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(layoutInflater.inflate(R.layout.item_message_push, null));

    }

    /**
     * 得到类型
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case PUSH:
                currentType = PUSH;
                break;
            case SYS:
                currentType = SYS;
                break;
            default:
                currentType = CHAT;
                break;
        }
        return currentType;
    }

    /***
     * 相当于getView中的绑定数据模块
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;

        if (getItemViewType(position) == PUSH) {
            MessagechatBean.PushBean pushBean = messageChat.getPush().get(0);
            viewHolder.ivHead.setImageResource(R.drawable.message_push);
            viewHolder.tvName.setText("推送信息");
            viewHolder.tvContent.setText(pushBean.getContent());
            viewHolder.tvDate.setText(pushBean.getDate());
        } else if (getItemViewType(position) == SYS) {
            MessagechatBean.SystemBean systemBean = messageChat.getSystem().get(0);
            viewHolder.ivHead.setImageResource(R.drawable.message_system);
            viewHolder.tvName.setText("系统信息");
            viewHolder.tvContent.setText(systemBean.getContent());
            viewHolder.tvDate.setText(systemBean.getDate());

        } else if (getItemViewType(position) == CHAT){
            MessagechatBean.ChatBean chatBean = messageChat.getChat().get(position-2);
            Glide.with(context)
                    .load(chatBean.getHead_url())
                    .thumbnail(Glide.with(context).load(R.drawable.loading))
                    .into(viewHolder.ivHead);
            viewHolder.tvName.setText(chatBean.getName());
            viewHolder.tvContent.setText(chatBean.getContent());
            viewHolder.tvDate.setText(chatBean.getDate());
        }
    }


    @Override
    public int getItemCount() {
        return messageChat.getChat().size()+2;
    }


    static
    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_head)
        CircleImageView ivHead;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_content)
        TextView tvContent;
        @BindView(R.id.tv_date)
        TextView tvDate;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
